package libra;

import java.util.HashMap;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.annotation.NacosValue;

@RestController
public class TestController {
	/**
	 * 使用@NacosValue与@Value的效果一致，均能获取NacosConfig中的指定配置
	 * 原因是@EnableNacosConfig注解会把指定dataId的配置项拉取到应用，并封装为PropertySource对象添加到Environment对象中
	 * 所以@Value也能读取到相应的配置项。
	 */
	@NacosValue("${msg}")
	private String msg;
	// 'autoRefreshed'为动态刷新属性，默认为false(关闭)。注意!：该自动刷新针对的是'当前属性绑定'
	@NacosValue(value = "${refresh}", autoRefreshed = true)
	private String refresh;
	@Value("${version}")
	private Double version;
	
	@NacosInjected
	private ConfigService configService;
	@PostConstruct
	public void init() {
		// 获取NacosConfig的状态
		System.out.println( "configService.getServerStatus()->" + configService.getServerStatus() );
	}
	
	@RequestMapping("getData")
	public Object getDate(HttpServletRequest req, HttpServletResponse resp) {
		return new HashMap<>() {{
			put("resp_msg", msg);
			put("resp_refresh", refresh);
			put("resp_version", version);
		}};
	}
}
